//
//  UIViewController+MBProgressHUD.m
//  Epayment
//
//  Created by safiri on 2018/8/10.
//  Copyright © 2018年 safiri. All rights reserved.
//

#import "UIViewController+MBProgressHUD.h"
#import <objc/runtime.h>
#import <MBProgressHUD/MBProgressHUD.h>

@implementation UIViewController (MBProgressHUD)

#pragma mark - text hud

- (void)showHUDWithTextInNav:(NSString *)text afterDelayTimeHide:(float)time{
    if (self.navigationController.view == nil || text.length == 0) {
        return;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    [hud setMode:MBProgressHUDModeText];
    hud.detailsLabel.text = text;
    hud.bezelView.color = [self MBHudColor];
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:time];
}

- (void)showHUDWithText:(NSString *)text afterDelayTimeHide:(float)time{
    if (text.length > 0 && self.view) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [hud setMode:MBProgressHUDModeText];
        hud.detailsLabel.text = text;
        hud.bezelView.color = [self MBHudColor];
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:YES afterDelay:time];
    }
}

- (void)showHUDWithText:(NSString *)text inView:(UIView *)view afterDelayTimeHide:(float)time {
    if (text.length > 0 && view) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        [hud setMode:MBProgressHUDModeText];
        hud.detailsLabel.text = text;
        hud.bezelView.color = [self MBHudColor];
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:YES afterDelay:time];
    }
}

- (void)showAutoHideHudInView:(UIView *)view withText:(NSString *)text {
    [self showHUDWithText:text inView:view afterDelayTimeHide:2.0f];
}

- (void)showAutoHideHudWithText:(NSString *)text{
    [self showHUDWithText:text afterDelayTimeHide:2.0f];
}

- (void)showAutoHideHudInNavWithText:(NSString *)text{
    [self showHUDWithTextInNav:text afterDelayTimeHide:2.0f];
}
#pragma mark - waiting hud
- (void)showWaitingHUDInView:(UIView *)bgView{
    if (bgView == nil) {
        return;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:bgView animated:YES];
    [hud setMode:MBProgressHUDModeIndeterminate];
    hud.bezelView.color = [self MBHudColor];
    [hud setAlpha:0.8];
    hud.removeFromSuperViewOnHide = YES;
    self.waitingMBHUD = hud;
}
- (void)showWaitingHUDInView:(UIView *)bgView andWaitingHintString:(NSString *)hintString{
    if (bgView == nil) {
        return;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:bgView animated:YES];
    [hud setMode:MBProgressHUDModeIndeterminate];
    hud.detailsLabel.text = hintString;
    hud.bezelView.color = [self MBHudColor];
    [hud setAlpha:0.8];
    hud.removeFromSuperViewOnHide = YES;
    self.waitingMBHUD = hud;
}
- (void)hideWaitingHUDInView:(UIView *)bgView{
    if (bgView == nil) return;
    [MBProgressHUD hideHUDForView:bgView animated:YES];
}
#pragma mark - custom hud
- (void)showCustomHUDInView:(UIView *)bgView custom:(UIView *)customView text:(NSString *)text {
    if (bgView == nil) {
        return;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:bgView animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    hud.customView = customView;
    hud.square = YES;
    hud.label.text = text;
    hud.bezelView.color = [self MBHudColor];
    hud.alpha = 0.8;
    hud.removeFromSuperViewOnHide = YES;
}

- (void)showCustomHUDAutoHideInView:(UIView *)bgView custom:(UIView *)customView text:(NSString *)text {
    if (bgView == nil) return;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:bgView animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    hud.customView = customView;
    hud.square = YES;
    hud.label.text = text;
    hud.bezelView.color = [self MBHudColor];
    hud.alpha = 0.8;
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:2.5f];
}

- (void)hideCustomHUDInView:(UIView *)bgView{
    if (bgView == nil) return;
    [MBProgressHUD hideHUDForView:bgView animated:YES];
}

#pragma mark - progress hud
- (void)showProgressRingShapedHUDInSelfViewWaitText:(NSString *)text progress:(NSProgress *)progress {
    self.progressRingShapedMBHUD.label.text = text;
    self.progressRingShapedMBHUD.progressObject = progress;
}
- (void)showProgressHorizontalBarMBHUDHUDInSelfViewWaitText:(NSString *)text progress:(NSProgress *)progress {
    self.progressHorizontalBarMBHUD.label.text = text;
    self.progressHorizontalBarMBHUD.progressObject = progress;
}
- (void)hideProgressRingShapedHUDInSelfView {
    [self.progressRingShapedMBHUD hideAnimated:YES];
}
- (void)hideProgressHorizontalBarHUDInSelfView {
    [self.progressHorizontalBarMBHUD hideAnimated:YES];
}

- (void)hideHUDInView:(UIView *)bgView {
    if (bgView == nil) return;
    [MBProgressHUD hideHUDForView:bgView animated:YES];
}
#pragma mark - getters and setters

- (UIColor *)MBHudColor {
    return [UIColor colorWithWhite:0.877 alpha:1];
}

- (void)setWaitingMBHUD:(MBProgressHUD *)waitingMBHUD {
    if (self.waitingMBHUD) {
        [self.waitingMBHUD hideAnimated:NO];
    }
    objc_setAssociatedObject(self, @selector(waitingMBHUD), waitingMBHUD, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (MBProgressHUD *)waitingMBHUD {
    return objc_getAssociatedObject(self, _cmd);
}



- (MBProgressHUD *)progressRingShapedMBHUD {
    MBProgressHUD *progressMBHUD = objc_getAssociatedObject(self, _cmd);
    if (progressMBHUD == nil) {
        progressMBHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        progressMBHUD.mode = MBProgressHUDModeAnnularDeterminate;
        progressMBHUD.bezelView.color = [self MBHudColor];
        progressMBHUD.alpha = 0.8;
        progressMBHUD.removeFromSuperViewOnHide = YES;
        objc_setAssociatedObject(self, @selector(progressRingShapedMBHUD), progressMBHUD, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return progressMBHUD;
}

- (MBProgressHUD *)progressHorizontalBarMBHUD {
    MBProgressHUD *progressMBHUD = objc_getAssociatedObject(self, _cmd);
    if (progressMBHUD == nil) {
        progressMBHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        progressMBHUD.mode = MBProgressHUDModeDeterminateHorizontalBar;
        progressMBHUD.bezelView.color = [self MBHudColor];
        progressMBHUD.alpha = 0.8;
        progressMBHUD.removeFromSuperViewOnHide = YES;
        objc_setAssociatedObject(self, @selector(progressHorizontalBarMBHUD), progressMBHUD, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return progressMBHUD;
}

@end
